package com.xxxx.server.utils;

import com.xxxx.server.pojo.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FastDFSClient {
    private static Logger logger= LoggerFactory.getLogger(FastDFSClient.class);

    //初始化
    static {
        try {
        String filePath =new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            logger.error("FastDFS Client init Fail!",e);
        }
    }

    private static TrackerServer getTrackerServer() throws IOException{

        return new TrackerClient().getTrackerServer();
    }


    private static StorageClient getStorageClient() throws IOException{
        TrackerServer trackerServer = getTrackerServer();
        return new StorageClient(trackerServer,null);//第二个参数可以不传
    }


    public static String[] upload(FastDFSFile file) throws Exception {
        logger.info("File Name"+file.getName()+"File length"+file.getContent().length);
        //文件属性
        NameValuePair[] meta_list =new NameValuePair[1];
        meta_list[0]=new NameValuePair("author",file.getAuthor());
        long startTime = System.currentTimeMillis();
        String[] uploadResults =null;
        StorageClient storageClient=null;
        try{
            storageClient=getStorageClient();
            uploadResults=storageClient.upload_file(file.getContent(),file.getExt(),meta_list);
        }catch (Exception e){
            e.printStackTrace();
           logger.error("上传失败");
        }
        logger.info("上传时间"+System.currentTimeMillis());
        if (uploadResults==null||storageClient==null){
            logger.info("上传失败"+storageClient.getErrorCode());
        }
        logger.info("上传成功,group_name:"+uploadResults[0]+"  remoteFileName:"+uploadResults[1]);
        return uploadResults;
    }



    public static InputStream downFile(String groupName, String remoteFileName){
        StorageClient storageClient=null;
        try{
            storageClient=getStorageClient();
            byte[] bytes=storageClient.download_file(groupName, remoteFileName);
            InputStream inputStream=new ByteArrayInputStream(bytes);
            return inputStream;
        } catch (Exception e) {
           logger.error("下载失败",e);
        }

        return null;
    }

    public static void deletedFiles(String groupName,String remoteFileName){
        StorageClient storageClient=null;
        try{
            storageClient=getStorageClient();
            storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            logger.error("删除失败",e);
        }
    }



    public static FileInfo getFile(String groupName, String remoteFileName){
        StorageClient storageClient =null;
        try{
            storageClient=getStorageClient();
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
           logger.error("查看文件失败",e);
        }
        return null;
    }


    public static String getTrackerUrl(){
        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer= null;
        StorageServer storageServer=null;
        try{
            trackerServer =trackerClient.getTrackerServer();
            storageServer=trackerClient.getStoreStorage(trackerServer);//追踪器
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "http://"+storageServer.getInetSocketAddress().getHostString()+":80/";//此端口号是nginx设置
    }

}
